package com.theaf.core.error;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 业务日志记录器
 * 
 * @author tone
 *
 */
@Component
public class BussinessLogger {

	@Resource(name = ThreadContext.NAME)
	ThreadContext context;

	@SuppressWarnings("rawtypes")
	private Log getLog(Object o) {
		if (o instanceof String) {
			return LogFactory.getLog((String) o);
		}
		if (o instanceof Class) {
			return LogFactory.getLog((Class) o);
		} else {
			return LogFactory.getLog(o.getClass());
		}
	}

	public void debug(Object o, Object message) {
		if (getLog(o).isDebugEnabled()) {
			getLog(o).debug(bussinessLogContent(message));
		}
	}

	public void debug(Object o, Object message, Throwable arg1) {
		if (getLog(o).isDebugEnabled()) {
			getLog(o).debug(bussinessLogContent(message), arg1);
		}
	}

	public void error(Object o, Object message) {
		if (getLog(o).isErrorEnabled()) {
			getLog(o).error(bussinessLogContent(message));
		}
	}

	public void error(Object o, Object message, Throwable arg1) {
		if (getLog(o).isErrorEnabled()) {
			getLog(o).error(bussinessLogContent(message), arg1);
		}
	}

	public void fatal(Object o, Object message) {
		if (getLog(o).isFatalEnabled()) {
			getLog(o).fatal(bussinessLogContent(message));
		}

	}

	public void fatal(Object o, Object message, Throwable arg1) {
		if (getLog(o).isFatalEnabled()) {
			getLog(o).fatal(bussinessLogContent(message), arg1);
		}
	}

	public void info(Object o, Object message) {
		if (getLog(o).isInfoEnabled()) {
			getLog(o).info(bussinessLogContent(message));
		}
	}

	public void info(Object o, Object message, Throwable arg1) {
		if (getLog(o).isInfoEnabled()) {
			getLog(o).info(bussinessLogContent(message), arg1);
		}
	}

	public void trace(Object o, Object message) {
		if (getLog(o).isTraceEnabled()) {
			getLog(o).trace(bussinessLogContent(message));
		}
	}

	public void trace(Object o, Object message, Throwable arg1) {
		if (getLog(o).isTraceEnabled()) {
			getLog(o).trace(bussinessLogContent(message), arg1);
		}
	}

	public void warn(Object o, Object message) {
		if (getLog(o).isWarnEnabled()) {
			getLog(o).warn(bussinessLogContent(message));
		}
	}

	public void warn(Object o, Object message, Throwable arg1) {
		if (getLog(o).isWarnEnabled()) {
			getLog(o).warn(bussinessLogContent(message), arg1);
		}
	}

	private String bussinessLogContent(Object message) {
		StringBuilder bulider = new StringBuilder(message == null ? "" : message.toString());
		bulider.append(System.getProperty("line.separator"));
		bulider.append(this.context.toString());
		return bulider.toString();
	}
}